package action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

import service.ProductService;
import action.form.ProductAddActionForm;

import com.opensymphony.xwork2.ActionSupport;

import entity.Product;

@Scope("request")
@ParentPackage(value = "default")
@Results(
    {
        @Result(name = "success", location = "product.jsp"),
        @Result(name = "redirect", type="redirectAction", location="product.html")
    }
)
public class ProductAction extends ActionSupport implements ServletRequestAware {
	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 5813278422008541411L;

	@Autowired
	private ProductAddActionForm productAddActionForm;

	@Autowired
	private ProductService productService;
	
	private List<Product> productList;
	
    private HttpServletRequest request;
	@Override
	public String execute() throws Exception {
		this.setProductList(productService.selectProductList());
		return SUCCESS;
	}
	
	@Action("/deleteProduct")
	public String delete() throws Exception {
		String id = getRequest().getParameter("id");
		productService.deleteProduct(Integer.parseInt(id));
		return "redirect";
	}
	
	public String add() {
		return SUCCESS;
	}

	/**
	 * @return the productList
	 */
	public List<Product> getProductList() {
		return productList;
	}

	/**
	 * @param productList the productList to set
	 */
	public void setProductList(List<Product> productList) {
		this.productList = productList;
	}

	/**
	 * @return the request
	 */
	public HttpServletRequest getRequest() {
		return request;
	}

	/**
	 * @param request the request to set
	 */
	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		this.request = arg0;
	}
	
}
